package org.example.userservice.controller;

import org.example.feign.pojo.*;
import org.example.userservice.mapper.Mapper;
import org.example.userservice.service.PojoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@CrossOrigin("*")
@RestController

public class PojoController {
    private final PojoService pojoService;

    @Autowired
    public PojoController(PojoService pojoService) {
        this.pojoService = pojoService;
    }
    @GetMapping("/pojo")

    public Result Pojo(
            @RequestParam(name = "param2", required = true) String param2
    )

    {
        return  pojoService.getPojo(param2);
    }

    @GetMapping("/getprice")
    public Result getprice() throws IOException {

        List<Price> price =pojoService.mybatisopen().getPrice();

        pojoService.mybatisclose();

        return Result.success(price);
    }
    @PostMapping("/sendPay")
    public String pay(@RequestBody  Pay pay){
        System.out.println(pay);
        int result= pojoService.mybatisopen().insertPay(pay);
        System.out.println("影响行数："+result);
        pojoService.mybatisclose();
        return "成功";

    }
    @PostMapping("/1/UpdataPrice")
    public String updataPrice(@RequestBody MyEntity entity){
        System.out.println(entity);
            int result=pojoService.mybatisopen().updataPrice(entity);
            System.out.println("影响行数："+result);
            pojoService.mybatisclose();
        return "更改成功";
    }
    @PostMapping("/newprice")
    public String newprice(@RequestBody MyEntity entity){
        System.out.println(entity);
        int result=pojoService.mybatisopen().insertPrice(entity);
        System.out.println("影响行数："+result);
        pojoService.mybatisclose();
        return "新建成功";
    }
    @PostMapping("/1/check")
    public  Employees checkLogin(@RequestBody Map<String, Object> requestBody){
        Employees employees =pojoService.mybatisopen().findByPhoneAndPassword(requestBody.get("number").toString(),
                requestBody.get("password").toString());

        System.out.println(employees);
        pojoService.mybatisclose();
        return employees;
    };

    @PostMapping("/data")
     public int Updata(@RequestBody Midpojo midpojo){
         Customer customer=midpojo.getCustomer();
         String openid=customer.getOpenid();
         int id=midpojo.getId();

        Mapper M = pojoService.mybatisopen();
        Customer existingCustomer = M.getCustomerByOpenid(openid);
        System.out.println(existingCustomer);
        int uID = 0;
        if (existingCustomer != null) {
            // 如果存在相同openid的记录，则更新其他用户信息
            if (id==4){
                existingCustomer.setGender(customer.getGender());
                existingCustomer.setPhone(customer.getPhone());
                existingCustomer.setAge(customer.getAge());
                existingCustomer.setName(customer.getName());
            }else if (id==2){

                existingCustomer.setNickName(customer.getNickName());
                existingCustomer.setAvatarUrl(customer.getAvatarUrl());

            }
            M.updateCustomer(existingCustomer);
            int result = M.updateCustomer(existingCustomer);
            uID = existingCustomer.getuID();
            System.out.println("影响行数11：" + result);
        } else {
            // 如果不存在相同openid的记录，则写入数据库
            int result = M.insertCustomer(customer);
            System.out.println("影响行数：" + result);
        }
        pojoService.mybatisclose();
        // 返回JsCode2SessionResponse对象
        return uID;
    }
}
